home *** CD-ROM | disk | FTP | other *** search
Wrap
Public Class KeyboardMouseForm Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Friend WithEvents txtNumber As System.Windows.Forms.TextBox Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents txtUpperCase As System.Windows.Forms.TextBox Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Label3 As System.Windows.Forms.Label Friend WithEvents txtDate As System.Windows.Forms.TextBox Friend WithEvents lblInfo As System.Windows.Forms.Label Friend WithEvents btnOK As System.Windows.Forms.Button Friend WithEvents btnCancel As System.Windows.Forms.Button Friend WithEvents chkCausesValidation As System.Windows.Forms.CheckBox 'Required by the Windows Form Designer Private components As System.ComponentModel.Container 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Label3 = New System.Windows.Forms.Label() Me.txtNumber = New System.Windows.Forms.TextBox() Me.chkCausesValidation = New System.Windows.Forms.CheckBox() Me.btnCancel = New System.Windows.Forms.Button() Me.btnOK = New System.Windows.Forms.Button() Me.lblInfo = New System.Windows.Forms.Label() Me.txtDate = New System.Windows.Forms.TextBox() Me.txtUpperCase = New System.Windows.Forms.TextBox() Me.Label1 = New System.Windows.Forms.Label() Me.Label2 = New System.Windows.Forms.Label() Me.SuspendLayout() ' 'Label3 ' Me.Label3.Location = New System.Drawing.Point(16, 160) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(288, 16) Me.Label3.TabIndex = 4 Me.Label3.Text = "Date field (press Shift-F2 for current date)" ' 'txtNumber ' Me.txtNumber.Location = New System.Drawing.Point(16, 32) Me.txtNumber.Name = "txtNumber" Me.txtNumber.Size = New System.Drawing.Size(288, 24) Me.txtNumber.TabIndex = 1 Me.txtNumber.Text = "" ' 'chkCausesValidation ' Me.chkCausesValidation.CausesValidation = False Me.chkCausesValidation.Checked = True Me.chkCausesValidation.CheckState = System.Windows.Forms.CheckState.Checked Me.chkCausesValidation.Location = New System.Drawing.Point(16, 128) Me.chkCausesValidation.Name = "chkCausesValidation" Me.chkCausesValidation.Size = New System.Drawing.Size(304, 16) Me.chkCausesValidation.TabIndex = 8 Me.chkCausesValidation.Text = "CausesValidation property of 2nd textbox" ' 'btnCancel ' Me.btnCancel.CausesValidation = False Me.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel Me.btnCancel.Location = New System.Drawing.Point(328, 80) Me.btnCancel.Name = "btnCancel" Me.btnCancel.Size = New System.Drawing.Size(72, 32) Me.btnCancel.TabIndex = 7 Me.btnCancel.Text = "Cancel" ' 'btnOK ' Me.btnOK.Location = New System.Drawing.Point(328, 32) Me.btnOK.Name = "btnOK" Me.btnOK.Size = New System.Drawing.Size(72, 32) Me.btnOK.TabIndex = 7 Me.btnOK.Text = "OK" ' 'lblInfo ' Me.lblInfo.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblInfo.Dock = System.Windows.Forms.DockStyle.Bottom Me.lblInfo.Location = New System.Drawing.Point(0, 229) Me.lblInfo.Name = "lblInfo" Me.lblInfo.Size = New System.Drawing.Size(424, 24) Me.lblInfo.TabIndex = 6 ' 'txtDate ' Me.txtDate.Location = New System.Drawing.Point(16, 184) Me.txtDate.Name = "txtDate" Me.txtDate.Size = New System.Drawing.Size(288, 24) Me.txtDate.TabIndex = 5 Me.txtDate.Text = "" ' 'txtUpperCase ' Me.txtUpperCase.Location = New System.Drawing.Point(16, 96) Me.txtUpperCase.Name = "txtUpperCase" Me.txtUpperCase.Size = New System.Drawing.Size(288, 24) Me.txtUpperCase.TabIndex = 3 Me.txtUpperCase.Text = "" ' 'Label1 ' Me.Label1.Location = New System.Drawing.Point(16, 8) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(296, 16) Me.Label1.TabIndex = 0 Me.Label1.Text = "Numeric value (must be non-empty)" ' 'Label2 ' Me.Label2.Location = New System.Drawing.Point(16, 72) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(176, 16) Me.Label2.TabIndex = 2 Me.Label2.Text = "Uppercase string" ' 'KeyboardMouseForm ' Me.AcceptButton = Me.btnOK Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17) Me.CancelButton = Me.btnCancel Me.ClientSize = New System.Drawing.Size(424, 253) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.chkCausesValidation, Me.btnCancel, Me.btnOK, Me.lblInfo, Me.Label3, Me.txtDate, Me.txtUpperCase, Me.Label2, Me.Label1, Me.txtNumber}) Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.HelpButton = True Me.KeyPreview = True Me.Name = "KeyboardMouseForm" Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen Me.Text = "Keyboard and Mouse" Me.ResumeLayout(False) End Sub #End Region Private Sub KeyboardMouseForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load InitializeMouseEvents(Me) End Sub ' a KeyPress event handler that ignores anything that isn't ' a digit key or a control character Private Sub txtNumber_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNumber.KeyPress If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then e.Handled = True End If End Sub ' a KeyPress handler that converts the char to uppercase Private Sub txtUpperCase_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtUpperCase.KeyPress ' Replace the selected text with an uppercase character txtUpperCase.SelectedText = e.KeyChar.ToString.ToUpper ' Cancel standard processing e.Handled = True End Sub ' this KeyDown event handler replaces the selection with current 'date if the user presses the Shift-F2 key Private Sub txtDate_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDate.KeyDown If e.Shift And (e.KeyCode = Keys.F2) Then txtDate.SelectedText = Today.Date.ToString e.Handled = True End If End Sub ' form-level key handler Private Sub KeyboardMouseForm_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown Dim t As String = "" ' build the status text If e.Alt Then t = "Alt " If e.Control Then t &= "Control " If e.Shift Then t &= "Shift " If t.Length <> 0 Then t = "(" & t.Trim & ") " t &= String.Format("KeyCode={0} KeyData={1} KeyValue={2}", e.KeyCode, e.KeyData, e.KeyValue) lblInfo.Text = t If Me.Controls.Count = 0 Then ' Ignore forms without any control. ElseIf e.KeyCode = Keys.Up Or e.KeyCode = Keys.Down Then ' Start at the currently selected control. Dim ctrl As Control = Me.ActiveControl ' Decide the direction once and for all. Dim moveForward As Boolean = (e.KeyCode = Keys.Down) Do ' Get the next control in that direction. ctrl = Me.GetNextControl(ctrl, moveForward) ' GetNectControl(ctrl,False) can return Nothing if first control. If Not (ctrl Is Nothing) AndAlso ctrl.CanFocus AndAlso ctrl.TabStop Then ' If the control can receive the focus, give it. ctrl.Focus() Exit Do End If Loop End If End Sub ' display a message box when F1 is pressed inside txtDate field Private Sub txtDate_HelpRequested(ByVal sender As Object, ByVal hlpevent As System.Windows.Forms.HelpEventArgs) Handles txtDate.HelpRequested MessageBox.Show("Insert a date", "Help Requested", MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub ' display mouse information when it is moved or clicked on the form Private Sub Form_MouseEvent(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown, MyBase.MouseUp, MyBase.MouseMove, MyBase.MouseWheel lblInfo.Text = String.Format("Mouse: Buttons={0} X={1} Y={2} Clicks={3} Delta={4}", e.Button, e.X, e.Y, e.Clicks, e.Delta) End Sub ' change and restore background color when the mouse ' enter/exits a control Private Sub MouseEnterEvent(ByVal sender As Object, ByVal e As System.EventArgs) DirectCast(sender, Control).BackColor = Color.Yellow End Sub Private Sub MouseLeaveEvent(ByVal sender As Object, ByVal e As System.EventArgs) DirectCast(sender, Control).BackColor = Color.White End Sub ' initialize the mouse events for all textbox and combobox ' controls on the form Sub InitializeMouseEvents(ByVal frm As Windows.Forms.Form) Dim ctrl As Control For Each ctrl In frm.Controls If (TypeOf ctrl Is TextBox) Or (TypeOf ctrl Is ComboBox) Then AddHandler ctrl.MouseEnter, AddressOf MouseEnterEvent AddHandler ctrl.MouseLeave, AddressOf MouseLeaveEvent End If Next End Sub ' This event proves a bug with the validation schema Private Sub txtNumber_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtNumber.Validating ' don't validate if the form is closing If isClosing Then Exit Sub If txtNumber.Text = "" Then e.Cancel = True Beep() lblInfo.Text = "First Textbox is required" Else lblInfo.Text = "First TextBox's Validation event has fired" End If End Sub ' close the form when OK and Cancel buttons are clicked Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click CloseForm() End Sub Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click CloseForm() End Sub Private Sub chkCausesValidation_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkCausesValidation.CheckedChanged txtUpperCase.CausesValidation = chkCausesValidation.Checked End Sub Dim isClosing As Boolean ' this procedure closes the form. It sets the closing variable ' so that other validation routines don't prevent the closing Sub CloseForm() isClosing = True Me.Close() End Sub End Class